﻿/****************************** Module Header ******************************\
 * Module Name:  FormEditor.cs
 * Module Version:  1.0.0 (2012-9-28)
 * Copyright (c) AnyEB.Com (14507247@qq.com)
 * 
 * This class is used to process the FormEditor.
 * 
 * This source is subject to the GPL License.
 * See http://www.gnu.org/licenses/gpl.html.
 * All other rights reserved.
 * 
 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
 * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\***************************************************************************/
using System;
using System.Windows.Forms;

namespace UCL.Forms.Components
{
    public partial class SoEditor : UserControl
    {
        /// <summary>
        /// 获取当前的WebBrowser控件
        /// </summary>
        public WebBrowser WebBrowser
        {
            get { return this.wbMain; }
        }

        /// <summary>
        /// 获取或设置编辑器资源目录
        /// </summary>
        public string ResourceExtraPath { get; set; }

        private string uploaderPath;

        public SoEditor()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 初始化编辑器
        /// <param name="uploaderUri">上传路径：如果是iframe跨域上传，需要在路径前加!,如 Init("!"+HttpUtility.UrlEncode(http://a.com/upload.aspx?ext=jpg|png"));</param>
        /// </summary>
        public void Init(string uploaderUri = "")
        {
            // 初始化默认属性
            if (string.IsNullOrEmpty(this.ResourceExtraPath)) this.ResourceExtraPath = Application.StartupPath + "\\Resource\\";

            if(!System.IO.Directory.Exists(this.ResourceExtraPath)) this.UnZipEditor();
            wbMain.Url = new Uri(ResourceExtraPath + "editor/Editor.htm");
            uploaderPath = uploaderUri;
            this.InvokeReadyScript("InitEditor", wbMain.Width - 17, wbMain.Height - 3, uploaderUri);
        }

        /// <summary>
        /// 获取编辑器的值
        /// </summary>
        /// <returns></returns>
        public string GetValue()
        {
            object s = wbMain.Document.InvokeScript("GetEditorVal");
            return s == null ? string.Empty : s.ToString();
        }

        /// <summary>
        /// 设置编辑器的值
        /// </summary>
        /// <param name="v"></param>
        public void SetValue(string v)
        {
            if (v == null) return;
            this.InvokeReadyScript("SetEditorVal", new object[] { v });
        }

        /// <summary>
        /// 等待文档加载完成后调用脚本
        /// </summary>
        /// <param name="scriptname"></param>
        /// <param name="args"></param>
        private void InvokeReadyScript(string scriptname, params object[] args)
        {
            if (wbMain.ReadyState == WebBrowserReadyState.Complete)
            {
                wbMain.Document.InvokeScript(scriptname, args);
            }
            else
            {
                wbMain.DocumentCompleted += (object s, WebBrowserDocumentCompletedEventArgs e) =>
                {
                    wbMain.Document.InvokeScript(scriptname, args);
                };
            }
        }

        /// <summary>
        /// 解压编辑器资源
        /// </summary>
        private void UnZipEditor()
        {
            if (FileHelper.FolderExists(ResourceExtraPath, false)) return;
            FileHelper fh = new FileHelper();
            fh.UnZip(ComRes.SoRichTextBox, ResourceExtraPath);
        }
    }
}
